Bug: Missing Stake-Weighted Aggregation in Consensus Score Calculation#32
Bug: Missing Stake-Weighted Aggregation in Consensus Score Calculation#32glowsenior wants to merge 1 commit intoPlatformNetwork:mainfrom
Conversation
…ection Replace simple arithmetic mean with stake-weighted consensus score calculation as documented in README. This fix addresses a critical security and correctness issue where all validators were treated equally regardless of stake. Changes: - Add calculate_stake_weighted_consensus_score() function - Implement stake-weighted average: sum(stake * score) / sum(stake) - Add outlier detection using z-score threshold (2.0) - Look up validator stakes from database for each evaluation - Handle edge cases (missing validators, zero stake, empty evaluations) Security Impact: - Restores Sybil resistance by weighting validators by stake - Prevents manipulation through outlier detection - High-stake validators now have appropriate influence Fixes: Missing stake-weighted aggregation in consensus score calculation Related: README.md lines 254-270 (Score Aggregation section) Before: Simple mean - scores.iter().sum() / scores.len() After: Stake-weighted with outlier filtering
📝 WalkthroughWalkthroughA new private function Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Bug: Missing Stake-Weighted Aggregation in Consensus Score Calculation
Summary
The
update_leaderboardfunction incrates/platform-server/src/db/queries.rscalculates consensus scores using a simple arithmetic mean instead of the documented stake-weighted average. This undermines the security model and doesn't match the documented behavior.Severity
High - Security and correctness issue
Location
crates/platform-server/src/db/queries.rsDescription
The consensus score calculation was using a simple arithmetic mean:
However, the README and documentation specify that scores should be aggregated using stake-weighted averaging with outlier detection:
Where:
Impact
Expected Behavior
According to
README.md(lines 254-270):Actual Behavior
The code was calculating a simple arithmetic mean without:
Root Cause
Evaluationstruct doesn't include validator stake informationSolution
Implemented
calculate_stake_weighted_consensus_score()function that:sum(stake * score) / sum(stake)Code Changes
Before
After
Testing Recommendations
Related Documentation
README.mdlines 152-180 (Validator operations)README.mdlines 252-279 (Score Aggregation)AGENTS.mdline 87 (Stake-weighted averaging)Summary by CodeRabbit
Release Notes
✏️ Tip: You can customize this high-level summary in your review settings.